import 'package:get/get.dart';
import 'package:vape_xe/routes/app_pages.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/extension.dart';

class AccountInfoController extends GetxController {
  /// 响应式成员变量
  final _nickName = ''.obs;

  set nickName(value) => _nickName.value = value;

  get nickName => _nickName.value;

  final _avatar = ''.obs;

  set avatar(value) => _avatar.value = value;

  get avatar => _avatar.value;


  /// 事件
  void onTap(int type) {
    //0手机 1邮箱
    if (type == 0) {
      String? mobile = UserStore.to.profile.mobile;
      if (mobile != null && mobile.isNotEmpty) {
        Get.toNamed(Routes.ACCOUNTINFODETAIL, arguments: {'type': 0});
      } else {
        Get.toNamed(Routes.ACCOUNTINFOCHANGE, arguments: {'type': 0});
      }
    } else if (type == 1) {
      String? email = UserStore.to.profile.email;
      if (email != null && email.isNotEmpty) {
        Get.toNamed(Routes.ACCOUNTINFODETAIL, arguments: {'type': 1});
      } else {
        Get.toNamed(Routes.ACCOUNTINFOCHANGE, arguments: {'type': 1});
      }
    }
  }

  /// 生命周期
  @override
  void onInit() {
    super.onInit();
    nickName = UserStore.to.profile.nickName ?? '';
    avatar = UserStore.to.profile.avatar ?? '';
    ever(EventService.to.message, (message) async {
      if (message.startsWith('${EventService.EventUpdate}_${EventService.ModuleNickname}')) {
        nickName = message.substringAfter('${EventService.EventUpdate}_${EventService.ModuleNickname}_');
        UserLoginResponseEntity loginInfo = UserStore.to.profile;
        loginInfo.nickName = nickName;
        UserStore.to.saveProfile(loginInfo);
      }
    });
  }
}
